//
//  JJFileHelper.m
//  ClaimPlatform_RN
//
//  Created by woshiku on 2018/6/26.
//  Copyright © 2018年 jj. All rights reserved.
//

#import "JJFileHelper.h"
#import "JJRNFileOperation.h"

@implementation JJFileHelper
+(SNFileHelper*)shared{
    static JJFileHelper *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[[self class] alloc] init];
    });
    return sharedMyManager;
}

-(void)downloadFileWithURLString:(NSString*)urlString finish:(FinishBlock)finish{
    NSLog(@"download with url:%@",urlString);
    NSURL * url = [NSURL URLWithString:urlString];
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
    NSURLSessionDownloadTask * downloadTask =[defaultSession downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error)
                                              {
                                                  if(error == nil)
                                                  {
                                                      NSLog(@"Temporary file =%@",location);
                                                      NSError *err = nil;
                                                      NSFileManager *fileManager = [NSFileManager defaultManager];
                                                      NSString *bundleZipPath=[NSString stringWithFormat:@"%@/\%@",[SNRNFileOperation getRnBundleTempPath],BUNDLEZIP];
                                                      NSURL *bundleZipDirURL = [NSURL fileURLWithPath:bundleZipPath];
                                                      if([fileManager fileExistsAtPath:bundleZipPath]){
                                                          [fileManager removeItemAtURL:bundleZipDirURL error:nil];
                                                      }
                                                      if ([fileManager moveItemAtURL:location
                                                                               toURL:bundleZipDirURL
                                                                               error: &err])
                                                      {
                                                          NSLog(@"File is saved to =%@",bundleZipDirURL.absoluteString);
                                                          finish(1,bundleZipPath);
                                                      }
                                                      else
                                                      {
                                                          NSLog(@"failed to move: %@",[err userInfo]);
                                                          finish(0,nil);
                                                      }
                                                  }
                                              }];
    [downloadTask resume];
}

@end
